"""
libgit2 MCP Server

面向 libgit2 软件的 MCP 服务器，提供对 libgit2 命令行操作的封装。

MCP tools 列表：
- libgit2_version: 检查 libgit2 版本信息
  参数：无
  返回：包含版本信息的统一 JSON 结构

- libgit2_clone: 克隆 Git 仓库
  参数：repository_url (str) - 要克隆的仓库 URL, target_dir (str) - 目标目录路径
  返回：包含克隆结果的统一 JSON 结构

- libgit2_init: 初始化 Git 仓库
  参数：directory (str) - 要初始化的目录路径
  返回：包含初始化结果的统一 JSON 结构

- libgit2_status: 查看仓库状态
  参数：repo_path (str) - 仓库路径
  返回：包含状态信息的统一 JSON 结构

- libgit2_log: 查看提交日志
  参数：repo_path (str) - 仓库路径, max_count (int) - 最大日志条数
  返回：包含日志信息的统一 JSON 结构

所有工具返回的统一 JSON 结构：
{
  "success": bool,
  "command": str,
  "exit_code": int,
  "stdout": str,
  "stderr": str
}
"""

from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List

mcp = FastMCP("libgit2 MCP Server")

@mcp.tool()
def libgit2_version() -> dict:
    """检查 libgit2 版本信息
    
    返回:
        dict: 包含版本检查结果的统一 JSON 结构
    """
    command = ["git", "--version"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def libgit2_clone(repository_url: str, target_dir: str) -> dict:
    """克隆 Git 仓库
    
    参数:
        repository_url (str): 要克隆的仓库 URL
        target_dir (str): 目标目录路径
    
    返回:
        dict: 包含克隆结果的统一 JSON 结构
    """
    command = ["git", "clone", repository_url, target_dir]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=300)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def libgit2_init(directory: str) -> dict:
    """初始化 Git 仓库
    
    参数:
        directory (str): 要初始化的目录路径
    
    返回:
        dict: 包含初始化结果的统一 JSON 结构
    """
    command = ["git", "init", directory]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def libgit2_status(repo_path: str) -> dict:
    """查看仓库状态
    
    参数:
        repo_path (str): 仓库路径
    
    返回:
        dict: 包含状态信息的统一 JSON 结构
    """
    command = ["git", "-C", repo_path, "status"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def libgit2_log(repo_path: str, max_count: int = 10) -> dict:
    """查看提交日志
    
    参数:
        repo_path (str): 仓库路径
        max_count (int): 最大日志条数，默认为 10
    
    返回:
        dict: 包含日志信息的统一 JSON 结构
    """
    command = ["git", "-C", repo_path, "log", f"--max-count={max_count}", "--oneline"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()